<?php
class koudai_qdControl extends skymvc{
	
	public function __construct(){
		
		parent::__construct();		
	}
	
	public function onDefault(){
		 
		$where=" shopid=".SHOPID."  AND status in(0,1,2,3,10) AND qdadmin=".ADMINID ;
		$url=APPKOUDAI."?m=koudai_order";
		$start=get('per_page','i');
		$limit=20;
		
		$orderno=get('orderno','h');
		if($orderno){
			$where.=" AND orderno='".$orderno."' ";
			$url.="&orderno=$orderno";
		}
		
		$nickname=get('ninckname','h');
		if($nickname){
			$user=M("user")->selectRow(array("where"=>"nickname='".$nickname."' "));
			if($user){
				$where.=" userid=".$user['userid'];
			}else{
				$where.=" 1=2 ";			
			}
			$url.="&nickname=".urlencode($nickname);
		}
		 
		$status=get('s_status','i');
		if($status>=0){
			$status_w=" AND status=$status";
			$url.="&s_status=$status";
		}
		if(!isset($_GET['s_ispay'])){
			$ispay=-1;
		}else{
			$ispay=get('s_ispay','i');
		}
		if($ispay>=0){
			$where.=" AND ispay=$ispay";
			$url.="&s_ispay=$ispay"; 
		}
		$start_time=get('start_time','h');
		$end_time=get('end_time','h');
		if($start_time){
			$where.=" AND dateline>".strtotime($start_time)." ";
			$url.="&start_time=".$start_time;
		}
		
		if($end_time){
			$where.=" AND dateline<".strtotime($end_time)." ";
			$url.="&end_time=".$end_time;
		}
		$type_id=get_post('type_id','i');
		if($type_id){
			$where.=" AND type_id=".$type_id." ";
			$url.="&type_id=".$type_id;
		}
		
		$where.=$status_w;
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_order")->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"order"=>"id DESC"));
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("koudai_qd/index.html");
	}
	
	
	public function onorder(){
		$status_w=" AND status in(0,1,2,3,10) AND qdadmin=0 ";
		$scids=M("koudai_admin_area")->selectCols(array(
			"fields"=>"sc_id",
			"where"=>" adminid=".ADMINID
		)); 
		$where=" shopid=".SHOPID."  ";
		if(!empty($scids)){
			$where.=" AND sc_id in("._implode($scids).") ";
		}else{
			$where.=" AND 1=2 ";
		}
		
		$url=APPKOUDAI."?m=koudai_order";
		$start=get('per_page','i');
		$limit=20;
		$where.=$status_w;
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_order")->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"order"=>"id DESC"));
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("koudai_qd/order.html");
	}
	
	public function orderproduct($order_id){
		
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".intval($order_id)));
		if($order_product){
			foreach($order_product as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			foreach($order_product as $k=>$v){
				$p=$products[$v['productid']];
				$p['order_price']=$v['price'];
				$p['amount']=$v['amount'];
				$p['total_money']=$v['amount']*$v['price'];
				$p['gznum']=$v['gznum'];
				$p['thnum']=$v['thnum'];
				$p['jsnum']=$v['jsnum'];
				$p['order_product_id']=$v['id'];
				$order_product[$k]=$p;
			}
		}
		return $order_product;	
	}
	
	public function onGet(){
		$order_id=get('order_id','i');
		$order=M("koudai_order")->selectRow("order_id=".$order_id);
		if($order['qdadmin']) $this->goAll("已有人签单了 签单失败",1);
		 
		 
		$data=array(
			"userid"=>$order['userid'],
			"shopid"=>$order['shopid'],
			"siteid"=>$order['siteid'],
			"adminid"=>ADMINID,
			"order_id"=>$order_id,
			"typeid"=>1,
			"dateline"=>time(),
		);
		M("koudai_qd")->insert($data);
		M("koudai_order")->update(array(
			"qdadmin"=>ADMINID
		),"order_id=".$order_id);
		$this->goAll("获取签单成功");
	}
	
}
?>